/*
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.docx4j.TraversalUtil;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.finders.RangeFinder;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.org.apache.poi.util.IOUtils;
import org.docx4j.wml.Body;
import org.docx4j.wml.CTBookmark;
import org.docx4j.wml.Document;
import org.docx4j.wml.Drawing;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.springframework.core.io.DefaultResourceLoader;

import com.thinkgem.jeesite.modules.DataService.util.ZipUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class SaveWord {
	private Configuration configuration = null;
	// private String templatePath = "/com/zhaochao";
	private String templatePath;
	private String fileTemplateName = "moban.ftl";
	private String wordGenPath;

	public SaveWord() {
		configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
	}

	public void createDoc(Map<String, Object> dataMap) throws IOException {
		// 1。 获取工程路径
		File projectPath = new DefaultResourceLoader().getResource("")
				.getFile();
		// 2.文件路径
		String separator = File.separator;
		String path = projectPath.getPath() + "/templates/word";
		templatePath = path.replace("/", separator);
		wordGenPath = templatePath + "/word.doc";
		// 3.文件
		File f = new File(templatePath);
		configuration.setDirectoryForTemplateLoading(f);
		Template t = null;
		File outFile = new File(wordGenPath);
		Writer out = null;
		try {
			t = configuration.getTemplate(fileTemplateName);
			t.setEncoding("utf-8");
			out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(outFile), "utf-8"));
			t.process(dataMap, out);
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.close();
	}

	public String createSjfwkbDoc(Map<String, Object> dataMap,String docx, String temp) {
		// 1。 获取工程路径
		Writer out = null;
		try {
			File projectPath = new DefaultResourceLoader().getResource("")
					.getFile();
			// 2.模板文件路径
			String separator = File.separator;
			String path = projectPath.getPath() + "/templates/word";
			templatePath = path.replace("/", separator);
			// 3.输出文件路径
			wordGenPath = templatePath + "/temp.doc";
			File outFile = new File(wordGenPath);
			out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(outFile), "utf-8"));
			File f = new File(templatePath);
			if(!f.exists()){
				f.createNewFile();
			}
			//4.输出文件
			configuration.setDirectoryForTemplateLoading(f);
			Template t = null;
			t = configuration.getTemplate(temp+"_"+fileTemplateName);
			t.setEncoding("utf-8");
			t.process(dataMap, out);
			out.close();
			String lastpath="";
			if("DataService_flood".equals(temp)){
				lastpath= templatePath + "/"+docx;
	            ZipInputStream zipInputStream = ZipUtils.wrapZipInputStream(new FileInputStream(new File(path+"/docx_model.zip")));
	            ZipOutputStream zipOutputStream = ZipUtils.wrapZipOutputStream(new FileOutputStream(new File(lastpath)));
	            String itemname = "word/document.xml";
	            ZipUtils.replaceItem(zipInputStream, zipOutputStream, itemname, new FileInputStream(new File(wordGenPath)));
	            try{
	                addImage(lastpath,path);
	            }catch(Exception e){
	            	e.printStackTrace();
	            }
			}else{
				lastpath=templatePath+"/"+docx.replace(".docx", ".doc");
				outFile.renameTo(new File(lastpath));
			}
            System.out.println("success");
			return lastpath;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				out.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			return "";
		}
	}
	public  boolean addImage(String filePath,String path) throws Exception{

        // 载入模板文件
        WordprocessingMLPackage wPackage = WordprocessingMLPackage.load(new FileInputStream(filePath));
        // 提取正文
        MainDocumentPart mainDocumentPart = wPackage.getMainDocumentPart();
        Document wmlDoc = (Document) mainDocumentPart.getJaxbElement();
        Body body = wmlDoc.getBody();
        // 提取正文中所有段落
        List<Object> paragraphs = body.getContent();
        // 提取书签并创建书签的游标
        RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
        new TraversalUtil(paragraphs, rt);

        // 遍历书签
        for (CTBookmark bm:rt.getStarts()) {
            // 这儿可以对单个书签进行操作，也可以用一个map对所有的书签进行处理
            if (new File(path+"/"+bm.getName()+".jpg").exists()){   
            	System.out.println(bm.getName());
                // 读入图片并转化为字节数组，因为docx4j只能字节数组的方式插入图片
                InputStream is = new FileInputStream(path+"/"+bm.getName()+".jpg");
                byte[] bytes = IOUtils.toByteArray(is);
                // 穿件一个行内图片
                BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wPackage, bytes);

                 // createImageInline函数的前四个参数我都没有找到具体啥意思，，，，
                 // 最有一个是限制图片的宽度，缩放的依据
                int width=5000;
                if("Title".equals(bm.getName())){
                	width=8000;
                }
                Inline inline = imagePart.createImageInline(null, null, 0,1, false, width);
                // 获取该书签的父级段落
                P p = (P)(bm.getParent());

                ObjectFactory factory = new ObjectFactory();
                // R对象是匿名的复杂类型，然而我并不知道具体啥意思，估计这个要好好去看看ooxml才知道
                R run = factory.createR();
                // drawing理解为画布？
                Drawing drawing = factory.createDrawing();
                drawing.getAnchorOrInline().add(inline);
                run.getContent().add(drawing);
                p.getContent().add(run);
            }
        }
        wPackage.save(new FileOutputStream(filePath));
		return true;
    }
	
	public static void main(String[] args) throws Exception {
		SaveWord sw=new SaveWord();
		sw.addImage("D:\\space\\util\\ServiceExpresss_91e66937-361b-40a0-88c8-afd9bb57da1b.docx","D:\\space\\util\\");
	}
}
*/